package com.shaoshaohuo.app.ui;

import java.util.ArrayList;
import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Point;
import com.shaoshaohuo.app.entity.PointEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.adapter.MyPointAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @ClassName MyPointsActivity
 * @Decription 我的积分
 * @2015-8-3 下午8:43:07
 */
public class MyPointsActivity extends BaseActivity implements IXListViewListener {

    private TopbarView mTopbarView;
    private XListView mListview;
    private TextView mTotalText;
    
    private TextView mDuihuanText;
    
    private List<Point> mDataList = new ArrayList<Point>();
    private int size = 10;
    private String action = ListViewAction.NEW;
    private String cursor = "";
    private MyPointAdapter<Point> mAdapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_points);
        initView();
        setUpView();
        requestData(true);
    }

    private void requestData(boolean b) {
        if(b){
            startLoadingDialog();
        }
        
        RequestService.getInstance().getPointList(this, size+"", action, cursor, PointEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    PointEntity entity = (PointEntity) resultData;
                    List<Point> list = entity.getData().getList();
                    mDataList.addAll(list);
                    
                    mTotalText.setText("总积分:"+entity.getData().getTotal());
                    
                    if ((mDataList.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
//                      mListview.setPullLoadEnable(false);
                        mListview.endLoadingMore("没有更多了");
                    }
                    setAdapter(mDataList);
                }else{
                    showToast(resultData.getMsg());
                }
                onLoad();
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(msg);
                onLoad();
            }
        });
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
        mListview = (XListView) this.findViewById(R.id.listview_data_list);
        mDuihuanText = (TextView) this.findViewById(R.id.textview_duihuan);
        mTotalText = (TextView) this.findViewById(R.id.textview_total);
    }

    private void setUpView() {
        mTopbarView.setCenterText("我的积分");
        mTopbarView.setLeftView(true, true);
        
        mDuihuanText.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyPointsActivity.this,PointsMallActivity.class);
                startActivity(intent);
            }
        });
        setListView();
    }
    
    protected void setAdapter(List<Point> mDataList2) {
        if(mAdapter == null){
            mAdapter = new MyPointAdapter<Point>(this, mDataList2, false);
            mListview.setAdapter(mAdapter);
        }else{
            mAdapter.setData(mDataList2);
        }
    }

    private void setListView() {
        mListview.setPullLoadEnable(true);
        mListview.setPullRefreshEnable(true);
        mListview.setXListViewListener(this);
        
        mListview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//              Coupon item = (Coupon) parent.getItemAtPosition(position);
//              mAdapter.setSelectPosition(position);
//              showToast(item.getTitle());
//              Intent intent = new Intent(MessageCenterActivity.this,OrderDetailActivity.class);
//              intent.putExtra(ExtraName.orderid, order.getBusinessorderid());
//              startActivity(intent);
            }
        });
    }
    
    @Override
    public void onRefresh() {
        if(mDataList.size()>0){
            cursor = mDataList.get(0).getCursor();
        }else {
            cursor = "";
        }
        action = ListViewAction.NEW;
        mListview.setPullLoadEnable(true);
        requestData(false);
    }

    @Override
    public void onLoadMore() {
        if(mDataList.size()>0){
            cursor = mDataList.get(mDataList.size()-1).getCursor();
        }else {
            cursor = "";
        }
        action = ListViewAction.HISTORY;
        requestData(false);
    }

    private void onLoad() {
        mListview.stopRefresh();
        mListview.stopLoadMore();
//      mListview.setRefreshTime(StringHelper.formatDate(System.currentTimeMillis()+""));
    }

}
